/**
 * Created by Henry on 15/6/21.
 */
Ext.define('Admin.controller.Global', {
    extend: 'Ext.app.Controller',

    requires: [
        'Admin.view.main.factory.ColumnsFactory',
        'Admin.view.main.factory.ModelFactory',
        'Admin.view.main.region.ContextMenu',
        'Admin.view.main.module.SimpleGrid',
        'Admin.view.main.module.SimpleLine',
        'Admin.view.thumbnails.Thumbnails'
    ],

    stores: [
        'Navigations',
        'Thumbnails',
        'Models',
        'Stations'
    ],

    config: {
        refs: {
            viewport: 'viewport',
            contentPanel: 'appcontent',
            navigationTree: 'appnavigation',
            thumbnails: {
                selector: 'thumbnails',
                xtype: 'thumbnails',
                autoCreate: true
            }
        },
        control: {
            'appcontent tab': {
                click: 'onContentTabClick'
            },
            navigationTree: {
                itemcontextmenu: 'onContextMenuClick',
                itemclick: 'onNavigationClick'
            },
            thumbnails: {
                itemclick: 'onThumbnailClick',
                itemdblclick: 'onThumbnailClick'
            }
        },
        routes: {
            ':id': {
                action: 'handleRoute',
                before: 'beforeHandleRoute'
            }
        }
    },

    beforeHandleRoute: function (id, action) {
        var me = this,
            store = me.getNavigationsStore();
        if (store.data.length > 1) {
            me.isNodeExist(id, action);
        } else {
            store.on('load', function () {
                me.isNodeExist(id, action);
            });
        }
    },

    isNodeExist: function (id, action) {
        var me = this,
            store = me.getNavigationsStore(),
            node = store.getNodeById(id);

        if (node) {
            //resume action
            action.resume();
        } else {
            Ext.Msg.alert(
                'Route Failure',
                'The view for ' + id + ' could not be found. You will be taken to the application\'s start',
                function () {
                    me.redirectTo(me.getApplication().getDefaultToken());
                }
            );

            //stop action
            action.stop();
        }
    },

    handleRoute: function (id) {
        var me = this,
            store = me.getNavigationsStore(),
            node = store.getNodeById(id),
            contentPanel = me.getContentPanel(),
            moduleId = 'module-' + node.getId(),
            panel = contentPanel.down('#' + moduleId),
            className, ViewClass, thumbnailsStore, alias;

        if (!node.isLeaf()) {
            thumbnailsStore = me.getThumbnailsStore();
            thumbnailsStore.removeAll();
            thumbnailsStore.add(node.childNodes);
        }

        if (!panel) {
            alias = node.isLeaf() ? node.get('module') : 'thumbnails';
            className = Ext.ClassManager.getNameByAlias('widget.' + alias);
            ViewClass = Ext.ClassManager.get(className);
            panel = new ViewClass({
                tabConfig: {
                    href: '#' + id,
                    hrefTarget: '_self'
                },
                itemId: moduleId,
                title: node.get('text'),
                glyph: parseInt(node.get('glyph'), 16),
                modelName: node.get('model'),
                url: node.get('url'),
                closable: true,
                reorderable: true
            });
            contentPanel.add(panel);
        }

        contentPanel.setActiveTab(panel);
        var msg = '打开【' + (node.get('text')) + '】模块';
        this.getViewport().getViewModel().set('status', msg);
    },

    onContentTabClick: function (view, e) {
        var href = view.getHref(),
            id = href.substring(1);
        this.redirectTo(id);
    },

    onNavigationClick: function (view, node, item, index, e) {
        this.redirectTo(node.getId());
    },

    onContextMenuClick: function (view, node, item, index, e) {
        e.preventDefault();
        e.stopEvent();
        var menu = Ext.widget('contextmenu');
        menu.showAt(e.getXY());
    },

    onThumbnailClick: function (view, node, item, index, e) {
        var navigationTree = this.getNavigationTree();
        navigationTree.ensureVisible(node, {
            select: true,
            focus: true
        });
        this.redirectTo(node.getId());
    }
});
